package com.photoeditor.demo.ui.adapter.effect;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;


import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.store.module.StoreOnlineBean;
import com.photoeditor.demo.util.APKAssertImageLoader;

import java.util.List;

/**
 * 滤镜商店详情页面 的 StickerAdapter
 */
public class StickerDetailAdapter extends ArrayAdapter<String>{

	private Activity mActivity;

	private StoreOnlineBean mStickerNetBean;

	private List<String> mData;

	private boolean mIsLocal;

	private int mItemWidth;

	private int mItemHeight;

	private boolean mLoadAssert;

	private String mRootUrl;

	private Resources mResources;

	/**
	 *
	 * @param context
	 * @param netBean
	 * @param itemWidth
	 * @param itemHeight
	 */
	public StickerDetailAdapter(Activity context, StoreOnlineBean netBean, int itemWidth, int itemHeight, boolean loadAssert){
		super(context, 0, netBean.getStickerUrls());
		this.mActivity = context;
		this.mData = netBean.getStickerUrls();
		mStickerNetBean = netBean;
		mIsLocal = false;
		mItemWidth = itemWidth;
		mItemHeight = itemHeight;
		mLoadAssert = loadAssert;
		if(mLoadAssert) {
			mRootUrl = StoreOnlineBean.removeQueryString(netBean.getRootUrl());
			try {
				mResources = ComponentContext.getContext().createPackageContext(netBean.getPkgName(), Context.CONTEXT_IGNORE_SECURITY).getResources();
			} catch (PackageManager.NameNotFoundException e) {
				e.printStackTrace();
			}

		}
	}

	@Override
	public int getCount() {
		if(mData != null) {
			return mData.size();
		} else{
			return 0;
		}
	}

	@Override
	public String getItem(int position) {
		if(position < mData.size()){
			return mData.get(position);
		}
		return null;
	}

	@Override
	public boolean isEmpty() {
		if(mData == null || mData.size() == 0){
			return true;
		}
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		return super.isEnabled(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = new ImageView(mActivity);
			((ImageView)convertView).setScaleType(ImageView.ScaleType.FIT_CENTER);
			GridView.LayoutParams lp = new GridView.LayoutParams(mItemWidth, mItemHeight);
			convertView.setLayoutParams(lp);
		}
		String url = mData.get(position);
		ImageView item = ((ImageView) convertView);
		item.setImageResource(R.drawable.ic_default_image);
		item.setScaleType(ImageView.ScaleType.FIT_XY);
		if(mLoadAssert && mResources != null) {
			String assertPath = StoreOnlineBean.removeQueryString(url).substring(mRootUrl == null ? 0 : mRootUrl.length() + 1);
			APKAssertImageLoader.with(mResources).load(assertPath).into(item);
		} else {
			ImageLoader.loadImage(mActivity, url, R.drawable.ic_default_image, item);
		}
		return convertView;
	}

	public boolean isLocal() {
		return mIsLocal;
	}

	public void destory(){
	}
}
